package com.bw.network_sdk.interceptor;

import com.blankj.utilcode.util.SPUtils;

import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 自定义token拦截器
 * */
public class TokenInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        //二次添加token生成新的request->获取response返回
        //获取本地sp存储中的token字符串
        String token = SPUtils.getInstance().getString("token");
        Request request = chain.request();
        //将原有Request请求中对应的地址，请求方式，请求参数，请求头
        //添加到新的Request.Builder
        Request.Builder builder = request.newBuilder();
        //向新的Builder中添加token 请求头
        builder.addHeader("token",token);
        return chain.proceed(builder.build());
    }
}
